package love.simbot.example.utils.wangyi;


import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.ktor.util.Hash;
import love.simbot.example.entity.wangyi.newSong.QQSongResult;
import love.simbot.example.entity.wangyi.newSong.WangYiSongResult;
import love.simbot.example.entity.wangyi.search.QQResult;
import love.simbot.example.entity.wangyi.search.SearchResult;
import love.simbot.example.entity.wangyi.songDetail.SongResult;
import love.simbot.example.utils.http.HttpUtils;

import java.util.HashMap;

/**
 * 网易云音乐的工具类
 */
public class MusicUtils {


    // 发送音乐请求的接口
    // 网易云
    private static final  String MUISC_HTTP1 = "https://music.cyrilstudio.top";
    private static final  String MUSIC_HTTP2 = "https://autumnfish.cn";

    private static final String MUSIC_HTTP4 = "https://yuanxiapi.cn/api/NetEaseCloud/";

    // QQ音乐
    private static final  String MUSIC_HTTP3 = "http://ovooa.com/API/QQ_Music/";

    private static final ObjectMapper om = new ObjectMapper();

    // 网易云音乐的搜索
    public static SearchResult SearchMusic(String keywords,Integer limit){
        HashMap<String,Object> params = new HashMap<>();
        String newKeyWords = StrUtil.cleanBlank(keywords);
        params.put("keywords",newKeyWords);
        params.put("limit",limit);
        String result = null;
        result = HttpUtils.getTool(MUSIC_HTTP2 + "/search", params);
        if(result == null|| result.contains("操作频繁")){
            result  = HttpUtils.getTool(MUISC_HTTP1 + "/search", params);
        }
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        SearchResult searchResult = null;
        try {
            searchResult = om.readValue(result, SearchResult.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return  searchResult;
    }

    // 网易云音乐的最后查找
    public static WangYiSongResult getMusicInfo(Long id) throws JsonProcessingException {
        HashMap<String,Object> params = new HashMap<>();
        params.put("id",id);
        String result = null;
        result = HttpUtils.getTool(MUSIC_HTTP4, params);
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        WangYiSongResult wangYiSongResult = om.readValue(result, WangYiSongResult.class);
        return wangYiSongResult;
    }


    // QQ音乐的查找
    public static QQResult getQQMusicInfo(String keywords) throws JsonProcessingException {
        // ?Skey=&uin=&msg=夜曲&n=1
        HashMap<String,Object> params = new HashMap<>();
        String newKeyWords = StrUtil.cleanBlank(keywords);
        params.put("Skey","");
        params.put("uin","");
        params.put("msg",newKeyWords);
        String result = HttpUtils.getTool(MUSIC_HTTP3, params);
        if(result == null){
            return null;
        }
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        QQResult qqResult = om.readValue(result, QQResult.class);
        return qqResult;
    }


    // QQ音乐的搜索
    public static QQSongResult getQQMusic(Integer index,String keywords) throws JsonProcessingException {
        HashMap<String,Object> params = new HashMap<>();
        String newKeyWords = StrUtil.cleanBlank(keywords);
        params.put("Skey",null);
        params.put("uin",null);
        params.put("msg",newKeyWords);
        params.put("n",index-5);
        String result = HttpUtils.getTool(MUSIC_HTTP3, params);
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        QQSongResult qqSongResult = om.readValue(result, QQSongResult.class);
        return qqSongResult;
    }


}
